<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog v-model="props.addVisible" class="add_regional_library" :title="props.dialogTitle" @close="handleVisible('cancel')">
    <el-form ref="formRef" size="small" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="区域编码" prop="regionCode">
        <el-input v-model="formData.regionCode" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="区域类型" prop="regionType">
        <el-input v-model="formData.regionType" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="几何形状" prop="shape">
        <el-input v-model="formData.shape" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="几何尺寸" prop="dimensions">
        <el-input v-model="formData.dimensions" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="边界条件" prop="boundaryCondition">
        <el-input v-model="formData.boundaryCondition" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="构建方式" prop="constructionMethod">
        <el-input v-model="formData.constructionMethod" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="区域坐标数据" prop="coordinateData">
        <el-input v-model="formData.coordinateData" :disabled="props.dialogTitle === '查看区域库'" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-model="formData.remarks"
          :disabled="props.dialogTitle === '查看区域库'"
          placeholder="请输入"
          type="textarea"
          rows="2"
          resize="none"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="handleVisible('cancel')">取消</el-button>
        <el-button size="small" type="primary" @click="submitAdd"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script name="AddRegionalLibrary" setup>
import { saveRegion } from '@/api/regionalLibrary';

const props = defineProps(['addVisible', 'rowData', 'dialogTitle']);
const emit = defineEmits(['handleEvent']);
const modalType = ref('');
if (props.dialogTitle === '新增区域库') {
  modalType.value = 'add';
} else if (props.dialogTitle === '修改区域库') {
  modalType.value = 'edit';
} else {
  modalType.value = 'view';
}

const formRef = ref(null);
const formData = ref({
  regionCode: '',
  regionType: '',
  thickness: '',
  symmetrical: '',
  PLY1: ''
});
const plys = ref([]);
if (modalType.value === 'edit' || modalType.value === 'view') {
  formData.value = props.rowData;
}

const rules = ref({
  regionCode: [{ required: true, message: '区域编码不能为空', trigger: 'blur' }],
  regionType: [{ required: true, message: '区域类型不能为空', trigger: 'blur' }],
  shape: [{ required: true, message: '几何形状不能为空', trigger: 'blur' }],
  dimensions: [{ required: true, message: '几何尺寸不能为空', trigger: 'blur' }],
  boundaryCondition: [{ required: true, message: '边界条件不能为空', trigger: 'blur' }],
  constructionMethod: [{ required: true, message: '构建方式不能为空', trigger: 'blur' }],
  coordinateData: [{ required: true, message: '区域坐标数据不能为空', trigger: 'blur' }],
  remarks: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
});

const handleVisible = (type) => {
  if (type === 'cancel') {
    emit('handleEvent', { type: 'close' });
  } else {
    emit('handleEvent', { type: 'refresh' });
  }
};
const submitAdd = () => {
  formRef.value.validate((valid) => {
    if (!valid) {
      return false;
    }
    if (modalType.value === 'edit' || modalType.value === 'add') {
      saveRegion(formData.value).then((res) => {
        if (res) {
          if (modalType.value === 'add') {
            console.log('RES', res);
            ElMessage({ showClose: true, message: '新增成功。', type: 'success' });
          } else {
            ElMessage({ showClose: true, message: '修改成功。', type: 'success' });
          }
          handleVisible();
        }
      });
    } else {
      handleVisible('cancel');
    }
  });
};
</script>
<style lang="scss" scoped>
.add_regional_library {
  user-select: none;
  .ply_item {
    width: 100%;
    display: flex;
    align-items: center;
    .ply_icon {
      font-size: 16px;
      margin-left: 10px;
      cursor: pointer;
    }
  }
}
</style>
